//
//  BDMError.m
//  iJobs
//
//  Created by Li Xiaopeng on 15/1/22.
//  Copyright (c) 2015年 liyingpeng. All rights reserved.
//

#import "BDMError.h"

@interface BDMError()

+ (NSString *)codeToMsg:(TBDMErrorCode)code msg:(NSString*)strMsg;

@end

@implementation BDMError

- (id)initWithCode:(TBDMErrorCode)code msg:(NSString*)strMsg
{
    self = [super init];
    if (self)
    {
       [BDMError codeToMsg:code msg:strMsg];
    }
    
    return self;
}

+ (NSString *)codeToMsg:(TBDMErrorCode)code msg:(NSString*)strMsg
{
    NSString* strValue = nil;
    switch (code)
    {
        case error_network_connet:
            strValue = NSLocalizedString(@"network_connet_error", @"");
            break;
        default:
            break;
    }
    
//    if (![BDMUtility isEmptyString:strMsg]) {
//        strValue = strMsg;
//    }
    
    return strValue;
}

+ (BDMError *)errorWithError:(NSError *)err
{
   TBDMErrorCode errorCode;
    
   NSString *errorMsg;
    // 跟网络返回相关的
    switch(err.code) {
        case -1009: {
            // 网络链接错误
            errorCode = error_network_connet;
            errorMsg = NSLocalizedString(@"network_connet_error", @"");
        }
            break;
        case 5: {
            errorCode = error_bad_url_request;
            errorMsg = NSLocalizedString(@"request_url_bad", @"");
        } break;
        case 1:
        case 2: {
            errorCode = error_network_connet;
            errorMsg = NSLocalizedString(@"network_connet_error", @"");
        }
            
            break;
        case 6: {
            errorCode = error_responds_no_data;
            errorMsg = NSLocalizedString(@"sv_responds_no_data", @"");
        }
            break;
        case -1001: {
            // 网络链接超时
            errorCode = error_connect_timeout;
            errorMsg = NSLocalizedString(@"network_connect_timeout", @"");
        }
            break;
        case -1011:
        case -1002: {
            // 请求的url不存在
            errorCode = error_requestUrl_not_exit;
            errorMsg = NSLocalizedString(@"request_url_not_exit", @"");
        }
            break;
        case 504:
        case 404: {
            errorCode = error_operation_could_not_complete;
            errorMsg = NSLocalizedString(@"operation_could_not_complete", @"");
        }
            break;
        case -999: {
            errorCode = error_network_connet;
            errorMsg = err.localizedDescription;
        }
            break;
        case -1013: {
            errorCode = error_authority_requried;
            errorMsg = err.localizedDescription;
        }
            break;
        default: {
            errorCode = error_network_connet;
            errorMsg = NSLocalizedString(@"network_connet_error", @"");
        }
            break;
    }
    return [BDMError errorWithDomain:kErrorDomain
                               code:errorCode
                           userInfo:[NSDictionary dictionaryWithObjectsAndKeys:errorMsg, NSLocalizedDescriptionKey, nil]];
}

+ (NSError *)errorWithCode:(TBDMErrorCode)code msg:(NSString*)strMsg
{
    return [BDMError errorWithDomain:kErrorDomain
                               code:code
                           userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[BDMError codeToMsg:code msg:strMsg], NSLocalizedDescriptionKey, nil]];
}

@end
